#!/bin/bash

# 选择操作
echo "请选择要执行的操作："
echo "1. 配置华为镜像源"
echo "2. 安装 Docker"
echo "3. 安装 Docker Compose"
read -p "请输入选项(1/2/3): " choice

configure_huawei_mirror() {
    source /etc/os-release
    if [ "$ID" == "centos" ]; then
        echo "开始配置华为镜像源..."
        cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
        wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo
        yum clean all
        yum makecache
        echo "华为镜像源配置完成！"
    else
        echo "不支持的操作系统！"
        exit 1
    fi
}

install_docker() {
    source /etc/os-release
    echo "开始安装 Docker..."
    case "$ID" in
        centos|rhel)
            yum remove -y docker docker-common docker-selinux docker-engine
            yum install -y yum-utils device-mapper-persistent-data lvm2
            wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
            sed -i 's+download.docker.com+mirrors.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
            yum makecache fast
            yum install -y docker-ce
            ;;
        debian|ubuntu)
            apt-get remove -y docker docker-engine docker.io
            apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
            curl -fsSL https://mirrors.huaweicloud.com/docker-ce/linux/$ID/gpg | apt-key add -
            add-apt-repository "deb [arch=amd64] https://mirrors.huaweicloud.com/docker-ce/linux/$ID $(lsb_release -cs) stable"
            apt-get update
            apt-get install -y docker-ce
            ;;
        *)
            echo "不支持的操作系统！"
            exit 1
            ;;
    esac
    echo "Docker 安装完成！"
}

install_docker_compose() {
    echo "开始安装 Docker Compose..."
    curl -L "https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    echo "Docker Compose 安装完成！"
}

case "$choice" in
    1) configure_huawei_mirror ;;
    2) install_docker ;;
    3) install_docker_compose ;;
    *) echo "无效的选项！" ;;
esac
